function loadAreas() 
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var id = "820315";
	
	if (zTree != null)
	{
		nodes = zTree.getSelectedNodes();
		
		if (nodes != null)
		{
			var flag = $("#flag").val();
			
			//alert("flag-->" + flag);
			
			if (flag == "add")
			{
				id = nodes[0].id;
			}
			else
			{
				id = nodes[0].pId;
			}
			
		}
		
	}
	
	$.post("areaAction!getAreaInfos.action", {inParma:JSON.stringify({"id":id})}
	, function(data)
	{
		data = eval("(" + data + ")");
		
		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			showBranches(data.data);
			
			$("#areaMenu").css({ "border-color": "black" }); 
		}
	});
}

var setting = {    
        check:{
            enable:true
        },        
        data:{
            simpleData:{
                enable:true
            }
        },
        callback:{
        	onRightClick:onRightClick
        }
    };

function showBranches(zNodeData)
{
	$.fn.zTree.init($("#units"), setting, zNodeData);
}

function onRightClick(event,treeId,treeNode)
{
	if (treeNode != null)
	{
		zTree = $.fn.zTree.getZTreeObj("units");
		zTree.selectNode(treeNode);
		//zTree.checkNode(treeNode,true);
		showRMenu(event.pageX, event.pageY);
		$("body").bind("mousedown", onBodyMouseDown);
	}
	
}

function onBodyMouseDown(event)
{  
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0))
    {  
    	$("#rMenu").css({ "visibility": "hidden" });
    }
    
    if (!(event.target.id == "inputDiv" || $(event.target).parents("#inputDiv").length > 0))
    {
        $("#inputDiv").css({ "visibility": "hidden" });
    }
}  

function showRMenu(x,y)
{
	$("#rMenu").show();
	$("#rMenu").css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
	
}

function showInput(x,y)
{
	$("#inputDiv").show();
	$("#inputDiv").css({ "top": y + "px", "left": x + "px", "visibility": "visible" });
	
}

function removeTreeNode()
{
	$("#rMenu").hide();
	$("#flag").val("del");
	delNode();
    
}

function editName()
{
	$("#rMenu").hide();
	
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var orgName = nodes[0].name;
	$("#areaName").val(orgName);
	
    //显示新增div  
    var tag = 0; 
    
    $("#flag").val("chg");
    
    $("#units").mousemove(function (e) 
	{  
        	if (tag == 0)
    		{
        		showInput(e.pageX, e.pageY);
        		$("#areaName").focus();
            	tag = tag + 1;
    		}
    });
}

function addTreeNode() 
{  
    $("#rMenu").hide();
    //显示新增div  
    var tag = 0; 
    
    $("#flag").val("add");
    
    $("#units").mousemove(function (e) 
		{  
        	if (tag == 0)
    		{
        		showInput(e.pageX, e.pageY);
        		$("#areaName").focus();
            	tag = tag + 1;
    		}
    });
}

function submit()
{
	var flag = $("#flag").val();
	var areaName = $("#areaName").val();
	
	if (areaName == "")
	{
		alert("名称不能为空！");
	}
	else
	{
		if(flag == "add")
		{
			addNode();		
		}
		else if (flag == "chg")
		{		
			updateNode();
		}
		else
		{
			alert("未识别的操作");
		}
	}
	
}

function getRoot() 
{
   var treeObj = $.fn.zTree.getZTreeObj("units");
    //返回一个根节点
   return treeObj.getNodesByFilter(function (node) { return node.level == 0 ;}, true);
}

function updateNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var id = nodes[0].id;
	var areaName = $("#areaName").val();
	
	$("#areaName").val("");
	$("#inputDiv").hide();
	
	$.post("areaAction!updateArea.action", {inParma:JSON.stringify({"areaName":areaName,"id":id})}
	, function(data)
	{
		data = eval("(" + data + ")");
						
		if (data.ok == 0)
		{
			alert("更新失败");
		}
		else
		{			
			alert("更新成功");
		}
		
		loadAreas();
	});
}

function delNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	
	if (nodes[0].isParent)
	{
		alert("请逐个删除下级单位后再删除该单位");
	}
	else
	{
		var id = nodes[0].id;
		
		$.post("areaAction!delArea.action", {inParma:JSON.stringify({"id":id})}
		, function(data)
		{
			data = eval("(" + data + ")");
							
			if (data.ok == 0)
			{
				alert("删除失败");
			}
			else
			{			
				alert("删除成功");
			}
			
			loadAreas();
		});
	}
	
}

function addNode()
{
	var zTree = $.fn.zTree.getZTreeObj("units");
	var nodes = zTree.getSelectedNodes();
	var pId = nodes[0].id;
	var areaName = $("#areaName").val();
		
	$("#areaName").val("");
	$("#inputDiv").hide();
	
	$.post("areaAction!addArea.action", {inParma:JSON.stringify({"areaName":areaName,"pId":pId})}
	, function(data)
	{
		data = eval("(" + data + ")");
						
		if (data.ok == 0)
		{
			alert("新增失败");
		}
		else
		{			
			alert("新增成功");
		}
		
		loadAreas();
	});
}
